package org.rrs.duker.net;

import android.content.Context;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONObject;
import org.rrs.duker.base.BaseApplication;

import java.util.Map;

import okhttp3.OkHttpClient;

/**
 * Created by meiming on 16-7-8.
 */
public class NetManager {
    private static NetManager mNetManager = null;
    private RequestQueue mRequestQueue;

    private static class NetManagerHolder {
        private static final NetManager INSTANCE = new NetManager(BaseApplication.getInstance().getApplicationContext());
    }

    /**
     * @param context
     */
    private NetManager(Context context) {
        mRequestQueue = Volley.newRequestQueue(context, new OkHttp3Stack(new OkHttpClient()));
    }

    /**
     * 单例模式（静态内部类）
     *
     * @return VolleyManager instance
     */
    public static NetManager newInstance() {
        return NetManagerHolder.INSTANCE;
    }

    private <T> Request<T> addRequest(Request<T> request) {
        return mRequestQueue.add(request);//添加请求到队列
    }

    public void requestString(int method, String url, final Map<String, String> headers, Response.Listener<String> listener, Response.ErrorListener errorListener){
        StringRequest stringRequest = new StringRequest(method, url, listener, errorListener){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return headers;
            }
        };
        mRequestQueue.add(stringRequest);
    }

    public void requestString(String url, final Map<String, String> headers, Response.Listener<String> listener, Response.ErrorListener errorListener){
        requestString(Request.Method.GET, url, headers, listener, errorListener);
    }

    public void requestJson(int method, String url, JSONObject jsonObject, final Map<String, String> headers, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener){
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(method, url, jsonObject, listener, errorListener){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return headers;
            }
        };
        mRequestQueue.add(jsonObjectRequest);
    }

    public void requestJson(String url, final Map<String, String> header, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener){
        requestJson(Request.Method.GET, url, null, header, listener, errorListener);
    }

    /**
     * 取消请求
     *
     * @param tag
     */
    public void cancel(Object tag) {
        mRequestQueue.cancelAll(tag);
    }
}
